using SmartHealthcare.Read.Api.Services;
using RBAC.Interstructrue;
using Microsoft.Extensions.Logging;

namespace SmartHealthcare.Read.Api.Examples
{
    /// <summary>
    /// 药品费用计算服务使用示例
    /// </summary>
    public class MedicineCostCalculationExample
    {
        private readonly MedicineCostCalculationService _medicineCostService;
        private readonly ILogger<MedicineCostCalculationExample> _logger;

        public MedicineCostCalculationExample(MedicineCostCalculationService medicineCostService, ILogger<MedicineCostCalculationExample> logger)
        {
            _medicineCostService = medicineCostService;
            _logger = logger;
        }

        /// <summary>
        /// 示例1：计算单个药品费用
        /// </summary>
        public async Task Example1_CalculateSingleMedicineCost()
        {
            _logger.LogInformation("=== 示例1：计算单个药品费用 ===");

            // 计算药品ID为1，数量为5，医保报销比例为80%的费用
            var result = await _medicineCostService.CalculateSingleMedicineCostAsync(1, 5, 80);

            if (result.Success)
            {
                _logger.LogInformation($"药品名称：{result.MedicineName}");
                _logger.LogInformation($"规格：{result.Specification}");
                _logger.LogInformation($"单价：{result.UnitPrice:C2}");
                _logger.LogInformation($"数量：{result.Quantity}");
                _logger.LogInformation($"总金额：{result.TotalAmount:C2}");
                _logger.LogInformation($"医保报销金额：{result.InsuranceAmount:C2}");
                _logger.LogInformation($"自费金额：{result.SelfPayAmount:C2}");
            }
            else
            {
                _logger.LogError($"计算失败：{result.ErrorMessage}");
            }
        }

        /// <summary>
        /// 示例2：计算多个药品费用
        /// </summary>
        public async Task Example2_CalculateMultipleMedicineCost()
        {
            _logger.LogInformation("=== 示例2：计算多个药品费用 ===");

            var medicineItems = new List<MedicineCostItem>
            {
                new MedicineCostItem { MedicineId = 1, Quantity = 5 },
                new MedicineCostItem { MedicineId = 2, Quantity = 3 },
                new MedicineCostItem { MedicineId = 3, Quantity = 2 }
            };

            var result = await _medicineCostService.CalculateMultipleMedicineCostAsync(medicineItems, 80);

            if (result.Success)
            {
                _logger.LogInformation($"总金额：{result.TotalAmount:C2}");
                _logger.LogInformation($"医保报销金额：{result.InsuranceAmount:C2}");
                _logger.LogInformation($"自费金额：{result.SelfPayAmount:C2}");
                _logger.LogInformation($"药品明细数量：{result.MedicineDetails.Count}");

                foreach (var detail in result.MedicineDetails)
                {
                    _logger.LogInformation($"  - {detail.MedicineName}：{detail.TotalAmount:C2}");
                }
            }
            else
            {
                _logger.LogError($"计算失败：{result.ErrorMessage}");
            }
        }

        /// <summary>
        /// 示例3：根据药品名称搜索并计算费用
        /// </summary>
        public async Task Example3_CalculateMedicineCostByName()
        {
            _logger.LogInformation("=== 示例3：根据药品名称搜索并计算费用 ===");

            var results = await _medicineCostService.CalculateMedicineCostByNameAsync("阿司匹林", 2, 70);

            if (results.Any(r => r.Success))
            {
                _logger.LogInformation($"找到 {results.Count(r => r.Success)} 个匹配的药品");

                foreach (var result in results.Where(r => r.Success))
                {
                    _logger.LogInformation($"  - {result.MedicineName}（{result.Specification}）：{result.TotalAmount:C2}");
                }
            }
            else
            {
                _logger.LogError($"未找到匹配的药品");
            }
        }

        /// <summary>
        /// 示例4：计算患者所有药品费用
        /// </summary>
        public async Task Example4_CalculatePatientMedicineCost()
        {
            _logger.LogInformation("=== 示例4：计算患者所有药品费用 ===");

            var result = await _medicineCostService.CalculatePatientMedicineCostAsync("MC2024001", 
                DateTime.Now.AddDays(-30), DateTime.Now);

            if (result.Success)
            {
                _logger.LogInformation($"患者卡号：{result.MedicalCard}");
                _logger.LogInformation($"总金额：{result.TotalAmount:C2}");
                _logger.LogInformation($"医保报销金额：{result.TotalInsuranceAmount:C2}");
                _logger.LogInformation($"自费金额：{result.TotalSelfPayAmount:C2}");
                _logger.LogInformation($"药品数量：{result.MedicineCount}");

                if (result.ExpenseDetails.Any())
                {
                    _logger.LogInformation("费用明细：");
                    foreach (var detail in result.ExpenseDetails.Take(5)) // 只显示前5条
                    {
                        _logger.LogInformation($"  - {detail.MedicineName}：{detail.TotalAmount:C2}（{detail.ExpenseTime:yyyy-MM-dd}）");
                    }
                }
            }
            else
            {
                _logger.LogError($"计算失败：{result.ErrorMessage}");
            }
        }

        /// <summary>
        /// 示例5：验证药品价格
        /// </summary>
        public async Task Example5_ValidateMedicinePrice()
        {
            _logger.LogInformation("=== 示例5：验证药品价格 ===");

            var result = await _medicineCostService.ValidateMedicinePriceAsync(1, 20.00m);

            if (result.IsValid)
            {
                _logger.LogInformation($"药品名称：{result.MedicineName}");
                _logger.LogInformation($"进价：{result.PurchasePrice:C2}");
                _logger.LogInformation($"当前价格：{result.CurrentPrice:C2}");
                _logger.LogInformation($"建议最低价格：{result.SuggestedMinPrice:C2}");
                _logger.LogInformation($"建议最高价格：{result.SuggestedMaxPrice:C2}");
                _logger.LogInformation("价格验证通过");
            }
            else
            {
                _logger.LogError($"价格验证失败：{result.ErrorMessage}");
            }
        }

        /// <summary>
        /// 运行所有示例
        /// </summary>
        public async Task RunAllExamples()
        {
            _logger.LogInformation("开始运行药品费用计算服务示例...");

            try
            {
                await Example1_CalculateSingleMedicineCost();
                await Task.Delay(1000); // 等待1秒

                await Example2_CalculateMultipleMedicineCost();
                await Task.Delay(1000);

                await Example3_CalculateMedicineCostByName();
                await Task.Delay(1000);

                await Example4_CalculatePatientMedicineCost();
                await Task.Delay(1000);

                await Example5_ValidateMedicinePrice();

                _logger.LogInformation("所有示例运行完成！");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "运行示例时发生错误");
            }
        }
    }
} 